<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="航班号" prop="flightNo">
        <el-input
          v-model="queryParams.flightNo"
          placeholder="请输入航班号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="航空公司" prop="airCompany">
        <el-input
          v-model="queryParams.airCompany"
          placeholder="请输入航空公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起飞日期" prop="departureDay">
        <el-date-picker clearable
          v-model="queryParams.departureDay"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择起飞日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="降落日期" prop="landingDay">
        <el-date-picker clearable
          v-model="queryParams.landingDay"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择降落日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="起飞时间" prop="departureTime">
        <el-date-picker clearable
          v-model="queryParams.departureTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择起飞时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="降落时间" prop="landingTime">
        <el-date-picker clearable
          v-model="queryParams.landingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择降落时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="起飞城市" prop="departureCity">
        <el-input
          v-model="queryParams.departureCity"
          placeholder="请输入起飞城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="降落城市" prop="landingCity">
        <el-input
          v-model="queryParams.landingCity"
          placeholder="请输入降落城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余座位数量" prop="leftSeat">
        <el-input
          v-model="queryParams.leftSeat"
          placeholder="请输入剩余座位数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头等舱剩余座位" prop="firstreMainder">
        <el-input
          v-model="queryParams.firstreMainder"
          placeholder="请输入头等舱剩余座位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经济舱剩余座位" prop="secondreMainder">
        <el-input
          v-model="queryParams.secondreMainder"
          placeholder="请输入经济舱剩余座位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头等舱价格" prop="firstrePrice">
        <el-input
          v-model="queryParams.firstrePrice"
          placeholder="请输入头等舱价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经济舱价格" prop="secondrePrice">
        <el-input
          v-model="queryParams.secondrePrice"
          placeholder="请输入经济舱价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改日期" prop="updateDate">
        <el-date-picker clearable
          v-model="queryParams.updateDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['flight:flight:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['flight:flight:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flight:flight:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flight:flight:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flightList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="航班id" align="center" prop="fid" />
      <el-table-column label="航班号" align="center" prop="flightNo" />
      <el-table-column label="飞机型号" align="center" prop="planeType" />
      <el-table-column label="航空公司" align="center" prop="airCompany" />
      <el-table-column label="起飞日期" align="center" prop="departureDay" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureDay, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="降落日期" align="center" prop="landingDay" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.landingDay, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="起飞时间" align="center" prop="departureTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="降落时间" align="center" prop="landingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.landingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="起飞城市" align="center" prop="departureCity" />
      <el-table-column label="降落城市" align="center" prop="landingCity" />
      <el-table-column label="剩余座位数量" align="center" prop="leftSeat" />
      <el-table-column label="头等舱剩余座位" align="center" prop="firstreMainder" />
      <el-table-column label="经济舱剩余座位" align="center" prop="secondreMainder" />
      <el-table-column label="头等舱价格" align="center" prop="firstrePrice" />
      <el-table-column label="经济舱价格" align="center" prop="secondrePrice" />
      <el-table-column label="航班状态:  0 正常, 1 停飞, 2 延误, 3 完成" align="center" prop="fStatus" />
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改日期" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flight:flight:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flight:flight:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改航班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="航班号" prop="flightNo">
          <el-input v-model="form.flightNo" placeholder="请输入航班号" />
        </el-form-item>
        <el-form-item label="航空公司" prop="airCompany">
          <el-input v-model="form.airCompany" placeholder="请输入航空公司" />
        </el-form-item>
        <el-form-item label="起飞日期" prop="departureDay">
          <el-date-picker clearable
            v-model="form.departureDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择起飞日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="降落日期" prop="landingDay">
          <el-date-picker clearable
            v-model="form.landingDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择降落日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="起飞时间" prop="departureTime">
          <el-date-picker clearable
            v-model="form.departureTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择起飞时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="降落时间" prop="landingTime">
          <el-date-picker clearable
            v-model="form.landingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择降落时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="起飞城市" prop="departureCity">
          <el-input v-model="form.departureCity" placeholder="请输入起飞城市" />
        </el-form-item>
        <el-form-item label="降落城市" prop="landingCity">
          <el-input v-model="form.landingCity" placeholder="请输入降落城市" />
        </el-form-item>
        <el-form-item label="剩余座位数量" prop="leftSeat">
          <el-input v-model="form.leftSeat" placeholder="请输入剩余座位数量" />
        </el-form-item>
        <el-form-item label="头等舱剩余座位" prop="firstreMainder">
          <el-input v-model="form.firstreMainder" placeholder="请输入头等舱剩余座位" />
        </el-form-item>
        <el-form-item label="经济舱剩余座位" prop="secondreMainder">
          <el-input v-model="form.secondreMainder" placeholder="请输入经济舱剩余座位" />
        </el-form-item>
        <el-form-item label="头等舱价格" prop="firstrePrice">
          <el-input v-model="form.firstrePrice" placeholder="请输入头等舱价格" />
        </el-form-item>
        <el-form-item label="经济舱价格" prop="secondrePrice">
          <el-input v-model="form.secondrePrice" placeholder="请输入经济舱价格" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改日期" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改日期">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">城市机场
信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddFlAirport">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteFlAirport">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="flAirportList" :row-class-name="rowFlAirportIndex" @selection-change="handleFlAirportSelectionChange" ref="flAirport">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="城市id" prop="cityId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cityId" placeholder="请输入城市id" />
            </template>
          </el-table-column>
          <el-table-column label="机场名称" prop="apAme" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.apAme" placeholder="请输入机场名称" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlight, getFlight, delFlight, addFlight, updateFlight } from "@/api/flight/flight";

export default {
  name: "Flight",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFlAirport: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 航班表格数据
      flightList: [],
      // 城市机场表格数据
      flAirportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flightNo: null,
        planeType: null,
        airCompany: null,
        departureDay: null,
        landingDay: null,
        departureTime: null,
        landingTime: null,
        departureCity: null,
        landingCity: null,
        leftSeat: null,
        firstreMainder: null,
        secondreMainder: null,
        firstrePrice: null,
        secondrePrice: null,
        fStatus: null,
        createDate: null,
        updateDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flightNo: [
          { required: true, message: "航班号不能为空", trigger: "blur" }
        ],
        planeType: [
          { required: true, message: "飞机型号不能为空", trigger: "change" }
        ],
        airCompany: [
          { required: true, message: "航空公司不能为空", trigger: "blur" }
        ],
        departureDay: [
          { required: true, message: "起飞日期不能为空", trigger: "blur" }
        ],
        landingDay: [
          { required: true, message: "降落日期不能为空", trigger: "blur" }
        ],
        departureTime: [
          { required: true, message: "起飞时间不能为空", trigger: "blur" }
        ],
        landingTime: [
          { required: true, message: "降落时间不能为空", trigger: "blur" }
        ],
        departureCity: [
          { required: true, message: "起飞城市不能为空", trigger: "blur" }
        ],
        landingCity: [
          { required: true, message: "降落城市不能为空", trigger: "blur" }
        ],
        leftSeat: [
          { required: true, message: "剩余座位数量不能为空", trigger: "blur" }
        ],
        firstreMainder: [
          { required: true, message: "头等舱剩余座位不能为空", trigger: "blur" }
        ],
        secondreMainder: [
          { required: true, message: "经济舱剩余座位不能为空", trigger: "blur" }
        ],
        firstrePrice: [
          { required: true, message: "头等舱价格不能为空", trigger: "blur" }
        ],
        secondrePrice: [
          { required: true, message: "经济舱价格不能为空", trigger: "blur" }
        ],
        fStatus: [
          { required: true, message: "航班状态:  0 正常, 1 停飞, 2 延误, 3 完成不能为空", trigger: "blur" }
        ],
        createDate: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        updateDate: [
          { required: true, message: "修改日期不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询航班列表 */
    getList() {
      this.loading = true;
      listFlight(this.queryParams).then(response => {
        this.flightList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fid: null,
        flightNo: null,
        planeType: null,
        airCompany: null,
        departureDay: null,
        landingDay: null,
        departureTime: null,
        landingTime: null,
        departureCity: null,
        landingCity: null,
        leftSeat: null,
        firstreMainder: null,
        secondreMainder: null,
        firstrePrice: null,
        secondrePrice: null,
        fStatus: 0,
        createDate: null,
        createBy: null,
        updateDate: null,
        updateBy: null
      };
      this.flAirportList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加航班";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fid = row.fid || this.ids
      getFlight(fid).then(response => {
        this.form = response.data;
        this.flAirportList = response.data.flAirportList;
        this.open = true;
        this.title = "修改航班";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.flAirportList = this.flAirportList;
          if (this.form.fid != null) {
            updateFlight(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlight(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fids = row.fid || this.ids;
      this.$modal.confirm('是否确认删除航班编号为"' + fids + '"的数据项？').then(function() {
        return delFlight(fids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 城市机场
序号 */
    rowFlAirportIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 城市机场
添加按钮操作 */
    handleAddFlAirport() {
      let obj = {};
      obj.cityId = "";
      obj.apAme = "";
      this.flAirportList.push(obj);
    },
    /** 城市机场
删除按钮操作 */
    handleDeleteFlAirport() {
      if (this.checkedFlAirport.length == 0) {
        this.$modal.msgError("请先选择要删除的城市机场数据");
      } else {
        const flAirportList = this.flAirportList;
        const checkedFlAirport = this.checkedFlAirport;
        this.flAirportList = flAirportList.filter(function(item) {
          return checkedFlAirport.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFlAirportSelectionChange(selection) {
      this.checkedFlAirport = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flight/flight/export', {
        ...this.queryParams
      }, `flight_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
